<?php
declare (strict_types = 1);

namespace app\controller;


use app\common\model\Content;
use app\common\model\Friend;
use think\facade\View;


class U extends Common
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $id = input("param.id",356);
        $user = \app\common\model\User::find($id);
        View::assign("user",$user);

        //用户发文总数
        $count = Content::where("uid",$id)
            ->where("status",1)
            ->count();
        $user["article_count"] = $count;

        //用户阅读量
        $count = Content::where("uid",$id)
            ->where("status",1)
            ->sum("hits");
        $user["hit_count"] = $count;

        //粉丝
        $fans = Friend::where("fid",$id)->count();
        $user["fans_count"] = $fans;

        //关注
        $fans = Friend::where("uid",$id)->count();
        $user["guanzhu_count"] = $fans;

        //最新文章
        $last_article = \app\common\model\Content::with(['log'])
            ->where("uid",$id)
            ->order("id DESC")
            ->limit(5)
            ->paginate();
        if(!$last_article){
            $last_article = [];
        }
        View::assign("list",$last_article);


        return  view();
    }

    //关注或者取消关注
    public function follow($uid)
    {
        $user = $this->user;
        $where["uid"] = $user["id"];
        $where["fid"] = $uid;
        $res = Friend::where($where)->find();
        if($res){
            $res->delete();
            return json(["code"=>201,"msg"=>"已取消关注"]);
        }else{
            Friend::create($where);
            return json(["code"=>200,"msg"=>"关注成功"]);
        }
    }

    public function test()
    {
        /*$list = \app\common\model\User::where("nickname","注册用户")
            ->select();


        foreach ($list as  $k => $v){
            ///public/static/head/109.png
            $res = \app\common\model\User::where("nickname","<>","注册用户")
                ->orderRaw("rand()")->find();
            \app\common\model\User::where("id",$v["id"])
                ->save(["nickname"=>$res["nickname"]]);


        }
        p($list);*/

    }

}
